package com.wowangz.cms.systemmanage.passwordpolicy.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.systemmanage.passwordpolicy.pojo.PasswordPolicy;
import com.wowangz.cms.systemmanage.passwordpolicy.service.PasswordPolicyService;

@Scope("prototype")
@Controller
public class PasswordPolicyController extends BaseController {

	public static class Response {
		public static final int ERROR = 2;
		public static final int OBJECT_NOT_EXIST = 2;
		public static final int STR_EMPTY = 2;
		public static final int SUCCESS = 1;
	}

	@RequestMapping(value = "/systemmanage/passwordpolicy/toPasswordpolicy.web")
	public String queryUserList(HttpServletRequest request, HttpServletResponse response, Model model) {
		PasswordPolicyService passwordPolicyService = (PasswordPolicyService) IssTransactionContainer.getInstance(
				PasswordPolicyService.class, false);
		List<PasswordPolicy> list = passwordPolicyService.queryPasswordPolicyList();
		model.addAttribute("list", list);
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			PasswordPolicy passwordPolicy = list.get(i);
			if (i < list.size() - 1) {
				sb.append(passwordPolicy.getId() + ",");
			} else {
				sb.append(passwordPolicy.getId());
			}
		}
		model.addAttribute("passwordIds", sb.toString());
		return "/WEB-INF/jsp/systemmanage/passwordpolicy/passwordpolicy";
	}

	@RequestMapping(value = "/systemmanage/passwordpolicy/savePasswordPolicy.json", method = RequestMethod.POST)
	public void savePasswordPolicy(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			PasswordPolicyService passwordPolicyService = (PasswordPolicyService) IssTransactionContainer.getInstance(
					PasswordPolicyService.class, false);
			String passwordIds = request.getParameter("passwordIds");
			if (StringUtils.isNotEmpty(passwordIds)) {
				String[] ids = passwordIds.split(",");
				for (int i = 0; i < ids.length; i++) {
					String id = ids[i];
					PasswordPolicy policy = passwordPolicyService.queryPasswordPolicyById(Long.parseLong(id));
					if (policy != null) {
						String value = request.getParameter(id + "_value");
						if (StringUtils.isNotEmpty(value)) {
							policy.setValue(value);
							passwordPolicyService.updatePasswordPolicy(policy);
							jsonMap.put("code", Response.SUCCESS);
							jsonMap.put("message", "密码规则修改成功");
						} else {
							jsonMap.put("code", Response.STR_EMPTY);
							jsonMap.put("message", "密码规则暂时无法修改");
							break;
						}
					} else {
						jsonMap.put("code", Response.OBJECT_NOT_EXIST);
						jsonMap.put("message", "密码规则暂时无法修改");
						break;
					}
				}
			} else {
				jsonMap.put("code", Response.STR_EMPTY);
				jsonMap.put("message", "密码规则暂时无法修改");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.getWriter().write(new Gson().toJson(jsonMap));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
